const express = require('express');
const cors = require('cors');
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data')
const Cookies = require('js-cookie');
const { exec } = require('child_process');

const app = express();
const port = 3000;

app.use(cors());
app.use(express.json());

// 处理启动应用程序的请求
app.post('/wake-app', (req, res) => {
    const exePath = req.body.exePath; // 假设前端POST请求中包含exe的路径
    exec(`start "" "${exePath}"`, (error) => {
        if (error) {
            console.error(`执行出错: ${error}`);
            return res.status(500).send('无法启动应用');
        }
        res.send('应用已启动');
    });
});

// 处理视频上传请求
app.post('/upload-video', (req, res) => {
    const filePath = req.body.filePath; // 从请求体中获取文件路径
    const serverUrl = 'http://192.168.1.4:8080/assess/testVideo/upload'; // Java服务器的上传地址

    const formData = new FormData();
    const videoData = fs.createReadStream(filePath);

    formData.append('file', videoData)
    axios.post(serverUrl, formData, {
        headers: {
            'Authorization': Cookies.get('Admin-Token'),
            ...formData.getHeaders()
        },
        maxContentLength: Infinity,
        maxBodyLength: Infinity
    }).then(response => {
        res.send(response.data.data)
    }).catch(err => {
        res.send(err)
    })
});

app.listen(port, () => {
    console.log(`Server is running at http://localhost:${port}`);
});
